


#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>	// file control
#include <termios.h>	// serial com
#include <errno.h>	// standard error
#include <time.h>	//time computing
#include <string.h>	//memset
#include <stdio.h>	//printf

#include "socket.h"	// my sockets packed

/*  ... some definitions
*/
#define SERIAL_BYTE_LENGTH_MAX          1024
#define VP_BYTE_LENGTH_MAX              SERIAL_BYTE_LENGTH_MAX
#define WV_SECONDS_IN_DAY               86400
#define VP_ACK                          0x06
#define VP_NAK                          0x21
#define VP_CANCEL                       0x1B
#define VP_BADCRC                       0x18
#define VP_CR                           0x0D
#define VP_LF                           0x0A
#define VP_RESPONSE_TIMEOUT             5000L
#define VP_PARM_DO_RXCHECK              "DO_RXCHECK"

/*  ... typedefs
*/
typedef unsigned char       UCHAR;
typedef unsigned short      USHORT;
typedef unsigned int        UINT;
typedef unsigned long       ULONG;
typedef unsigned long long  ULONGLONG;
typedef unsigned char       BOOL;


// define 'B' type records first - this is what we use internally
// 'A' < Firmware Version 04/24/2002 <= 'B'
typedef struct
{
	USHORT              date;
	USHORT              time;
	short               outTemp;
	short               highOutTemp;
	short               lowOutTemp;
	USHORT              rain;
	USHORT              highRainRate;
	USHORT              barometer;
	USHORT              radiation;
	USHORT              windSamples;
	USHORT              inTemp;
	UCHAR               inHumidity;
	UCHAR               outHumidity;
	UCHAR               avgWindSpeed;
	UCHAR               highWindSpeed;
	UCHAR               highWindDir;
	UCHAR               prevWindDir;
	UCHAR               UV;                 // extra data begins here
	UCHAR               ET;
	USHORT              highRadiation;
	UCHAR               highUV;
	UCHAR               fcstRule;
	UCHAR               leafTemp1;
	UCHAR               leafTemp2;
	UCHAR               leafWet1;
	UCHAR               leafWet2;
	UCHAR               soilTemp1;
	UCHAR               soilTemp2;
	UCHAR               soilTemp3;
	UCHAR               soilTemp4;
	UCHAR               recordType;
	UCHAR               extraHumid1;
	UCHAR               extraHumid2;
	UCHAR               extraTemp1;
	UCHAR               extraTemp2;
	UCHAR               extraTemp3;
	UCHAR               soilMoist1;
	UCHAR               soilMoist2;
	UCHAR               soilMoist3;
	UCHAR               soilMoist4;
}
__attribute__ ((packed)) ARCHIVE_RECORD;

//  ... define the messages we receive
typedef struct
{
	UCHAR               interval;
	USHORT              crc;
}
__attribute__ ((packed)) ARCHIVE_INTERVAL;

typedef struct
{
	USHORT              pages;
	USHORT              firstRecIndex;
	USHORT              crc;
}
__attribute__ ((packed)) DMPAFT_HDR;

typedef struct
{
	UCHAR               seqNo;
	ARCHIVE_RECORD      record[5];
	UCHAR               unused[4];
	USHORT              crc;
}
__attribute__ ((packed)) ARCHIVE_PAGE;



